import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mybase/base_new/help/JJShock.dart';
import 'package:mybase/base_new/mixin/JJClick.dart';
import 'package:mybase/base_new/mixin/JJModify.dart';

// 文本
class JJText extends StatelessWidget with JJModify, JJClick {
  /// 文字
  String? text;

  bool? xxx;

  /// 最大行数
  int? maxLines;

  /// 文本样式,优先不传递该值
  TextStyle? textStyle;

  /// 文本大小
  double? fontSize;

  /// 字间距
  double? letterSpacing;

  /// 单词间距
  double? wordSpacing;

  /// 文本颜色
  Color? fontColor;

  /// 文本字粗
  FontWeight? fontWeight;

  /// 文本行高
  double? fontLineHeight;

  /// 文本字体
  String? fontFamily;

  /// 文本对齐
  TextAlign? textAlign;

  /// 文本是否垂直居中
  StrutStyle? strutStyle;

  /// 下划线
  TextDecoration? textDecoration;

  /// 下划线颜色
  Color? textDecorationColor;

  /// 下划线样式
  TextDecorationStyle? textDecorationStyle;

  /// 下划线粗细
  double? textDecorationThickness;

  /// 文本 颜色 渐变,最好设置fontColor：Colors.white
  /**
   * 用法举例
   * LinearGradient(
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      colors: [Color(0xFF8F1FFF), Color(0xFFFF00FF)],
      )
   */
  Gradient? gradient;

  /// 文本搭边宽度: 默认0
  double? strokeWidth;

  /// 文本描边颜色
  Color? strokeColor;

  /// 文本描边 Shader
  Shader? strokeShader;

  /// 阴影
  List<Shadow>? shadows;

  Paint? foreground;

  /// 遮罩
  ShaderMask Function(Widget child)? shaderMask;

  /// 文本是否垂直居中
  bool isVerticalCenter;

  JJText({
    super.key,

    String? notes,
    // 当前组件属性
    required this.text,
    this.xxx,
    this.maxLines,
    this.textStyle,
    this.fontSize,
    this.fontColor,
    this.letterSpacing,
    this.wordSpacing,
    this.fontWeight,
    this.fontLineHeight,
    this.fontFamily,
    this.textAlign,
    this.strutStyle,
    this.textDecoration,
    this.textDecorationColor,
    this.textDecorationStyle,
    this.textDecorationThickness,
    this.shadows,
    this.foreground,
    this.gradient,
    this.strokeColor,
    this.strokeWidth,
    this.shaderMask,
    this.isVerticalCenter = false,
    this.strokeShader,

    // JJModify属性
    AlignmentGeometry? alignment,
    EdgeInsetsGeometry? padding,
    Color? color,
    Decoration? decoration,
    Decoration? foregroundDecoration,
    double? width,
    double? height,
    BoxConstraints? constraints,
    EdgeInsetsGeometry? margin,
    Matrix4? transform,
    AlignmentGeometry? transformAlignment,
    Clip clipBehavior = Clip.none,
    bool unconstrainedBox = false,

    // JJClick属性
    Function? onClick,
    Function()? onLongClick,
    bool? hiddenKeepFrame,
    bool? hiddenClearFrame,
    int? minClickIntervalMill,
    double? clickedAlpha,
    double? clickedScale,
    int? scaleMill,
    ShockType? shockType,
    bool? ignoreInteraction,
    bool? ignoreInteractionAll,
    String? clickName,
    ClickType? clickType,
  }) {
    // JJModify
    this.alignment = alignment;
    this.padding = padding;
    this.color = color;
    this.decoration = decoration;
    this.foregroundDecoration = foregroundDecoration;
    this.width = width;
    this.height = height;
    this.constraints = constraints;
    this.margin = margin;
    this.transform = transform;
    this.clipBehavior = clipBehavior;
    this.transformAlignment = transformAlignment;
    this.unconstrainedBox = unconstrainedBox;

    // JJClick
    this.onClick = onClick;
    this.onLongClick = onLongClick;
    this.hiddenKeepFrame = hiddenKeepFrame;
    this.hiddenClearFrame = hiddenClearFrame;
    this.minClickIntervalMill = minClickIntervalMill;
    this.clickedAlpha = clickedAlpha;
    this.clickedScale = clickedScale;
    this.scaleMill = scaleMill;
    this.shockType = shockType;
    this.ignoreInteraction = ignoreInteraction;
    this.ignoreInteractionAll = ignoreInteractionAll;
    this.clickName = clickName;
    this.clickType = clickType;
  }

  // 固定搭配
  @override
  Widget build(BuildContext context) {
    return clickBuild(context, modifyBuild(context));
  }

  // 当前组件UI
  @override
  Widget selfBuild(BuildContext context) {
    // 文本
    Widget? view;
    if ((strokeWidth ?? 0) > 0) {
      // 描边
      var paint = Paint()
        ..style = PaintingStyle.stroke
        ..strokeWidth = strokeWidth ?? 0
        ..shader = strokeShader
        ..color = strokeColor ?? Colors.transparent;

      view = Stack(
        children: [
          // 描边
          selfText(paint: paint),
          // 文本
          selfText(fontColor: fontColor, shadows: shadows)
        ],
      );
    } else {
      view = selfText(shadows: shadows, paint: foreground, fontColor: fontColor);
    }

    if (gradient != null) {
      view = ShaderMask(
        shaderCallback: (Rect bounds) {
          return gradient!.createShader(Offset.zero & bounds.size);
        },
        child: view,
      );
    }

    // 自定义 ShaderMask
    if (shaderMask != null) {
      view = shaderMask!(view);
    }

    return view;
  }

  Widget selfText({
    List<Shadow>? shadows,
    Paint? paint,
    Color? fontColor,
  }) {
    var child = Text(
      xxx == true ? "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx end" : text ?? "",
      //textScaler: TextScaler.noScaling,
      textScaleFactor: 1.0,
      maxLines: maxLines,
      overflow: TextOverflow.ellipsis,
      textAlign: textAlign,
      strutStyle: strutStyle,
      style: textStyle ??
          TextStyle(
            fontSize: fontSize,
            color: fontColor,
            fontWeight: fontWeight,
            height: fontLineHeight ?? 1.2,
            letterSpacing: letterSpacing,
            wordSpacing: wordSpacing,
            fontFamily: fontFamily,
            decoration: textDecoration,
            decorationColor: textDecorationColor,
            decorationStyle: textDecorationStyle,
            decorationThickness: textDecorationThickness,
            shadows: shadows,
            foreground: paint,
          ),
    );

    if (isVerticalCenter) {
      return Center(
        child: child,
      );
    }

    return child;
  }
}
